package com.xuecheng.test.freemarker.controller;

import com.xuecheng.test.freemarker.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * @author Kidy
 * @date 2020/6/20 21:21
 */
@Controller
@RequestMapping("/freemarker")
public class FreemarkerController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/banner")
    public ModelAndView indexBanner() {
        ModelAndView mv = new ModelAndView();
        List images = new ArrayList();
        Map result = restTemplate.getForObject("http://localhost:31001/cms/config/getmodel/5a791725dd573c3574ee333f", Map.class);
        mv.addAllObjects(result);
        mv.setViewName("index_banner");
        return mv;
    }

    @RequestMapping("/course")
    public ModelAndView course() {
        ModelAndView mv = new ModelAndView();
        List images = new ArrayList();
        Map result = restTemplate.getForObject("http://localhost:31200/course/courseview/4028e581617f945f01617f9dabc40000", Map.class);
        mv.addAllObjects(result);
        mv.setViewName("course");
        return mv;
    }

    @RequestMapping("/test1")
    public ModelAndView test01() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("name", "黑马程序员");
        Student stu1 = new Student();
        stu1.setName("小明");
        stu1.setAge(18);
        stu1.setMoney(1000.86f);
        stu1.setBirthday(new Date());
        Student stu2 = new Student();
        stu2.setName("小红");
        stu2.setMoney(200.1f);
        stu2.setAge(19);
        stu2.setBirthday(new Date());
        List<Student> friends = new ArrayList<>();
        friends.add(stu1);
        stu2.setFriends(friends);
        stu2.setBestFriend(stu1);
        List<Student> stus = new ArrayList<>();
        stus.add(stu1);
        stus.add(stu2);
        //向数据模型放数据
        mv.addObject("stus", stus);
        //准备map数据
        HashMap<String, Student> stuMap = new HashMap<>();
        stuMap.put("stu1", stu1);
        stuMap.put("stu2", stu2);
        //向数据模型放数据
        mv.addObject("stu1", stu1);
        //向数据模型放数据
        mv.addObject("stuMap", stuMap);
        mv.addObject("point", 102395457);
        mv.setViewName("test1");
        return mv;
    }
}
